package J20250330_real;

/**
 * Created with IntelliJ IDEA.
 * Description: 和根植物——https://www.lanqiao.cn/problems/110/learning/?page=1&first_category_id=1&difficulty=20&second_category_id=3
 * User: 王圆豪
 * Date: 2025-04-10
 * Time: 21:05
 */

import java.util.*;

public class Main14{
    static int[] dx = {-1,1,0,0};
    static int[] dy = {0,0,1,-1};
    static HashMap<Integer, Set<Integer>> map;
    static boolean[][] vis;
    static int m,n;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        m = sc.nextInt();
        n = sc.nextInt();
        int k = sc.nextInt();
        map = new HashMap<>();
        vis = new boolean[m][n];
        while(k-- != 0) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            if(!map.containsKey(a)) map.put(a, new HashSet<>());
            if(!map.containsKey(b)) map.put(b, new HashSet<>());
            map.get(a).add(b);
            map.get(b).add(a);
        }
        int ret = 0;
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(!vis[i][j]) {
                    dfs(i,j);
                    ret++;
                }
            }
        }
        System.out.println(ret);
        sc.close();
    }

    static void dfs(int x, int y) {
        vis[x][y] = true;
        for(int i = 0; i < 4; i++) {
            int px = x + dx[i],py = y + dy[i];
            int pos = x*n+y+1, pos2 = px*n+1+py;
            if(px>=0&&px<m&&py<n&&py>=0&&!vis[px][py]&&map.containsKey(pos)&&map.get(pos).contains(pos2)) {
                dfs(px,py);
            }
        }
    }
}